import 'package:client/common/event.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:client/model/chat_item_model.dart';
import 'package:client/provider/chat_list_provider.dart';

class ChatList extends StatefulWidget {
  // FriendOnline({super.key});
  ChatList({super.key});

  @override
  _ChatListState createState() => _ChatListState();
}

class _ChatListState extends State<ChatList> {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    var chatListView = Consumer<ChatListProvider>(
      builder: (context, provider, child) {
        List<ChatItemModel> chatItemList = provider.getList();
        var view = ListView.builder(
            itemCount: chatItemList.length,
            itemBuilder: (context, index) {
              var model = chatItemList[index];
              var name = model.name;
              var onLine = model.onLineText();
              var infoTip = model.infoTip;

              var rowView = [
                Expanded(
                    flex: 9,
                    child: TextButton(
                        onPressed: () {
                          bus.emit(EventBusSet.chatChange, model);
                        },
                        child: Text(name + onLine))),
              ];
              if (infoTip > 0) {
                var tip = Expanded(
                    flex: 1,
                    child: Container(
                      decoration: const BoxDecoration(
                        color: Colors.red,
                        shape: BoxShape.circle,
                      ),
                      height: 20,
                      width: 20,
                    ));
                rowView.add(tip);
              }
              var rowItem = Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: rowView,
              );

              return rowItem;
            });
        return view;
      },
    );

    var view = Column(children: [
      Expanded(child: chatListView)
      // friendList
    ]);

    return view;
  }
}
